#include<stdio.h>
#include<thread>

pthread_mutex_t mutex1=PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t mutex2=PTHREAD_MUTEX_INITIALIZER;

class Foo {

   public: 
   Foo()
   {
        pthread_mutex_lock(&mutex1);
        pthread_mutex_lock(&mutex2);
   }

   void one() 
   { 
        printf("one"); 
        pthread_mutex_unlock(&mutex1);
   }
   
   void two() 
   { 
        pthread_mutex_lock(&mutex1);
        printf("two"); 
        pthread_mutex_unlock(&mutex2);
   }
   
   void three() 
   { 
        pthread_mutex_lock(&mutex2);
        printf("three\n"); 
        pthread_mutex_unlock(&mutex1);
   }
};

void* F1(void*arge)
{
    Foo*fo=static_cast<Foo*> (arge);
    fo->one();
    return nullptr;
}

void* F2(void* arge)
{
    Foo*fo=static_cast<Foo*> (arge);
    fo->two();

    return nullptr;
}

void* F3(void*arge)
{
    Foo*fo=static_cast<Foo*> (arge);
    fo->three();
    return nullptr;
}

int main()
{
    Foo fo;
    pthread_t f1;
    pthread_t f2;
    pthread_t f3;
    pthread_create(&f1,nullptr,F1,&fo);
    pthread_create(&f2,nullptr,F2,&fo);
    pthread_create(&f3,nullptr,F3,&fo);

    pthread_join(f1,nullptr);
    pthread_join(f2,nullptr);
    pthread_join(f3,nullptr);

    return 0;
}